//
//  FanLiViewController.m
//  ShangChengMoBan
//
//  Created by yaoqi yaoqi on 16/10/28.
//  Copyright © 2016年 ping. All rights reserved.
//

#import "FanLiViewController.h"
#import "FanLiTableViewCell.h"
@interface FanLiViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *you;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *zuo;

@property (weak, nonatomic) IBOutlet UILabel *zuiJin;
@property (weak, nonatomic) IBOutlet UILabel *all;

@end

@implementation FanLiViewController
{
    UIView * _view;
    RWScrollView *_table;
    NSArray * _arr;
    NSMutableArray * _imageArr , * _taiArr,* _shuJuArr;
    int _page;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    _page = 1;
    self.view.backgroundColor = [UIColor colorWithHue:0.00 saturation:0.00 brightness:0.77 alpha:1.00];
    [self drawNavWithTitle:@"返利明细" WithColor:[UIColor colorWithHue:0.00 saturation:0.00 brightness:1.00 alpha:1.00]];
    NSDate *currentDate = [NSDate date];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM"];
      NSString *dateStr = [formatter stringFromDate:currentDate];
   NSArray *shiJian = [dateStr componentsSeparatedByString:@"-"];
    [NSString stringWithFormat:@"%d年%d月",[shiJian[0] intValue],[shiJian[1] intValue]-1];
     _arr = @[@"近3个月",@"本月",[NSString stringWithFormat:@"%d年%d月",[shiJian[0] intValue],[shiJian[1] intValue]-1],[NSString stringWithFormat:@"%d年%d月",[shiJian[0] intValue],[shiJian[1] intValue]-2]];
    _imageArr = [[NSMutableArray alloc]init];
     _taiArr = [[NSMutableArray alloc]init];
    [self table];
    [self tanKuang];
    [self drawBackButton];
    [self dingBu];
}
-(void)headRefresh{
    _page = 1;
    [self requestDataWith:1];
}
-(void)footerRefresh{
    _page ++;
    [self requestDataWith:0];
}
-(void)requestDataWith:(int)a{
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    [dic setObject:@"10" forKey:@"pageSize"];
    [dic setObject:[NSString stringWithFormat:@"%d",_page] forKey:@"pageNum"];[NetWorking NEWjieKouQingQiu:@"" ANDIP:IP And:dic anBlock:^(NSDictionary *dic, NSError *error) {
        [_table headerEndRefreshing];
        [_table footerEndRefreshing];
    if (!error) {
        if ([dic[Code] intValue] == 200) {
            if (a == 1) {
                _shuJuArr = [[NSMutableArray alloc]init];
            }
        }
    }
}];
}
-(void)dingBu{
    UIView * view = [[[NSBundle mainBundle]loadNibNamed:@"FanHeader" owner:self options:nil] lastObject];
    view.frame = CGRectMake(0, 65, KScreenWidth, 30);
    [self.view addSubview:view];
    if (KScreenWidth<350) {
        _you.constant = 45;
        _zuo.constant = 45;
    }
}
-(void)tanKuang{
    _view = [[UIView alloc]initWithFrame:CGRectMake(0,95, KScreenWidth, KScreenHeight-95)];
    _view.alpha = 0.0;
    _view.backgroundColor = [UIColor colorWithWhite:0 alpha:0.4];
    [self.view addSubview:_view];
    UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, 163)];
    view1.backgroundColor = [UIColor colorWithHue:0.00 saturation:0.00 brightness:0.77 alpha:1.00];
    [_view addSubview:view1];
    for (int  i =0; i<4; i++) {
        UIView * xuan = [[UIView alloc]initWithFrame:CGRectMake(0, 40*i+i+1, KScreenWidth, 40)];
        xuan.backgroundColor = [UIColor whiteColor];
        [view1 addSubview:xuan];
        UILabel * lable = [[UILabel alloc]initWithFrame:CGRectMake(15, 10, 200, 20)];
        lable.text = _arr[i];
        lable.font = [UIFont systemFontOfSize:12];
        lable.textColor = MaintextColor;
        [xuan addSubview:lable];
        [_taiArr addObject:lable];
        UIImageView * image = [[UIImageView alloc]initWithFrame:CGRectMake(KScreenWidth-30, 12.5, 15, 15)];
        [xuan addSubview:image];
        [_imageArr addObject:image];
        UIButton * btn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, 40)];
        btn.tag = i;
        [btn addTarget:self action:@selector(take:) forControlEvents:UIControlEventTouchUpInside];
        [xuan addSubview:btn];
    }
}
-(void)table{
    _table = [[RWScrollView alloc]initWithFrame:CGRectMake(0, 96, KScreenWidth, KScreenHeight-96) style:UITableViewStyleGrouped];
    [_table addHeaderWithTarget:self action:@selector(headRefresh)];
    [_table addFooterWithTarget:self action:@selector(footerRefresh)];
    [_table headerBeginRefreshing];
    [self.view addSubview:_table];
    _table.delegate = self;
    _table .dataSource = self;
    [_table registerClass:[FanLiTableViewCell class] forCellReuseIdentifier:@"cell"];
    _table.separatorStyle = UITableViewCellSelectionStyleNone;
  
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 5;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, 30)];
    view.backgroundColor = [UIColor colorWithHue:0.00 saturation:0.00 brightness:0.95 alpha:1.00];
    UILabel * lab = [[UILabel alloc]initWithFrame:CGRectMake(10, 5, 300, 20)];
    lab.text = @"本月";
    lab.font = [UIFont systemFontOfSize:13];
    lab.textColor = [UIColor lightGrayColor];
    [view addSubview:lab];
    return view;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 30;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    FanLiTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
      [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    [cell.image sd_setImageWithURL:[NSURL URLWithString:[self requestImageStr]]];
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 71;
}
-(void)take:(UIButton *)btn{
    int tag = (int)btn.tag;
    _zuiJin.text = _arr[tag];
    _all.textColor = [UIColor blackColor];
    _zuiJin.textColor = [UIColor redColor];
  
    for (int i = 0 ; i<4; i++) {
        UIImageView * iamge1 =_imageArr[i];
        iamge1.image = nil;
        UILabel *lable = _taiArr[i];
        lable.textColor = MaintextColor;
    }
    ((UIImageView *)_imageArr[tag]).image = [UIImage imageNamed:@"对号"];
    ((UILabel *)_taiArr[tag]).textColor = [UIColor redColor];
    [UIView animateWithDuration:0.4 animations:^{
        _view.alpha = 0.0;
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)suoYouy:(id)sender {
    _all.textColor = [UIColor redColor];
    _zuiJin.textColor = [UIColor blackColor];
    for (int i = 0 ; i<4; i++) {
        UIImageView * iamge1 =_imageArr[i];
        iamge1.image = nil;
        UILabel *lable = _taiArr[i];
        lable.textColor = MaintextColor;
    }
    [UIView animateWithDuration:0.4 animations:^{
        _view.alpha = 0.0;
    }];
}
- (IBAction)zuiJin:(id)sender {
    [UIView animateWithDuration:0.4 animations:^{
        if (_view.alpha == 0.0) {
            _view.alpha = 1.0;
        }else{
            _view.alpha = 0.0;
        }

    }];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
